package com.interview.one.controller;

import com.interview.one.common.BusinessException;
import com.interview.one.common.CommonRes;
import com.interview.one.common.EmBusinessError;
import com.interview.one.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/msg")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @PostMapping("/getList")
    public CommonRes getList(@RequestHeader String token) throws BusinessException {
        if ((token == null) || (token.equals(""))) {
            throw new BusinessException(EmBusinessError.NOT_LOGIN_FAIL);
        }

        List list = this.messageService.getMessageList(Integer.valueOf(token));
        if ((list == null) || (list.size() == 0)) {
            throw new BusinessException(EmBusinessError.NO_DATA_ERROR);
        }
        return CommonRes.create(list);
    }
}
